Skip to content

fix: Fix invoker to work when using dataclass with from_dict but dataclass… #9434

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 26, 2025

Conversation

sjrl
Copy link
Contributor

@sjrl sjrl commented May 23, 2025

Related Issues

  • fixes #issue-number

Proposed Changes:

Fix invoker to work when using dataclass with from_dict but dataclas is already given

Updated the component_invoker method to allow fully formed dataclass objects to pass through. Otherwise we silently remove the parameter value.

Originally found by @bilgeyucel when creating an tool using Agent + ComponentTool and calling

agent_tool.invoke(messages=[ChatMessage.from_user("Tell me the latest news about gpus")])

How did you test it?

Notes for the reviewer

Checklist

  • I have read the contributors guidelines and the code of conduct
  • I have updated the related issue with new insights and changes
  • I added unit tests and updated the docstrings
  • I've used one of the conventional commit types for my PR title: fix:, feat:, build:, chore:, ci:, docs:, style:, refactor:, perf:, test: and added ! in case the PR includes breaking changes.
  • I documented my code
  • I ran pre-commit hooks and fixed any issue

@sjrl sjrl requested a review from bilgeyucel May 23, 2025 12:32
@coveralls
Copy link
Collaborator

coveralls commented May 23, 2025

Pull Request Test Coverage Report for Build 15210934031

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • 26 unchanged lines in 3 files lost coverage.
  • Overall coverage decreased (-0.03%) to 90.056%

Files with Coverage Reduction New Missed Lines %
core/pipeline/async_pipeline.py 3 67.32%
tools/component_tool.py 7 92.39%
core/pipeline/base.py 16 93.51%
Totals Coverage Status
Change from base Build 15210166613: -0.03%
Covered Lines: 11338
Relevant Lines: 12590

💛 - Coveralls

@sjrl sjrl self-assigned this May 23, 2025
@sjrl sjrl marked this pull request as ready for review May 23, 2025 12:42
@sjrl sjrl requested review from a team as code owners May 23, 2025 12:42
@sjrl sjrl requested review from dfokina, anakin87 and vblagoje and removed request for a team May 23, 2025 12:42
@sjrl
Copy link
Contributor Author

sjrl commented May 23, 2025

@vblagoje I'd also appreciate your review on this since you worked on ComponentTool in the past

@github-actions github-actions bot added the type:documentation Improvements on the docs label May 23, 2025
Copy link
Member

@anakin87 anakin87 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks OK but I would like if @vblagoje can review too

@@ -159,15 +159,19 @@ def component_invoker(**kwargs):
target_type = get_args(param_type)[0] if get_origin(param_type) is list else param_type
if hasattr(target_type, "from_dict"):
if isinstance(param_value, list):
param_value = [target_type.from_dict(item) for item in param_value if isinstance(item, dict)]
resolved_param_value = [
target_type.from_dict(item) if isinstance(item, dict) else item for item in param_value
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sjrl - just one clarification - so the only difference to previous code state is that we now include other items in the list - that are not dicts?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah we keep other items in the list (or really all items in a list) that aren't dicts. If we don't do this and a user passes this

agent_tool.invoke(messages=[ChatMessage.from_user("Tell me the latest news about gpus")])

we actually run the agent_tool with

messages=[ChatMessage.from_user("Tell me the latest news about gpus")]
# turns into 
messages=[]
# then we run Agent Tool with an empty list of messages

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is relevant in two cases:

  • If a user is trying to use a tool manually with invoke or within a custom component
  • If we pass inputs_from_state to a Tool which are already fully formed dataclasses. E.g. If I pass the messages from State into the input of a Tool that accepts ChatMessages. I wouldn't expect the invoke function to then remove all the messages.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now it makes total sense 🙏

@vblagoje vblagoje self-requested a review May 26, 2025 07:27
Copy link
Member

@vblagoje vblagoje left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice find @bilgeyucel

@sjrl sjrl merged commit 0d23fe7 into main May 26, 2025
20 checks passed
@sjrl sjrl deleted the fix-component-tool-invoker branch May 26, 2025 07:40
julian-risch pushed a commit that referenced this pull request May 26, 2025
…class… (#9434)

* Fix invoker to work when using dataclass with from_dict but dataclass is already given

* add reno

* Add unit test

* Remove line
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic:tests type:documentation Improvements on the docs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants